<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row :gutter="24">
            <el-col :span="5">
              <el-form-item label="门店" prop="storeId">
                <el-select
                  v-model="queryParams.storeId"
                  placeholder="请选择"
                  filterable
                  clearable
                >
                  <el-option
                    v-for="(item, index) in $store.state.user.stores"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <div class="timebox">
                <el-form-item
                  label="banner类型"
                  prop="bannerType"
                  label-width="120"
                >
                  <el-select
                    class="w-160"
                    v-model="queryParams.bannerType"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in BannerTypeList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </div>
            </el-col>

            <!-- <el-col :span="6">
              <el-form-item label="时间" prop="time">
                <el-date-picker
                  v-model="queryParams.time"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
            </el-col> -->
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>
    <div class="table_card">
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">门店后台Banner</div>
        <div class="op_right">
          <button class="primary_button" @click="NewBanner">新增Banner</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="StoreBannerList" stripe style="width: 100%">
          <el-table-column prop="bannerSeq" label="轮播图排序">
          </el-table-column>
          <el-table-column prop="storeName" label="门店名称"> </el-table-column>
          <el-table-column prop="bannerUrl" label="图片地址">
            <template slot-scope="scope">
              <img class="imgBanner" :src="scope.row.bannerUrl" alt="" />
            </template>
          </el-table-column>
          <el-table-column prop="bannerType" label="轮播图类型">
            <template slot-scope="scope">
              {{ BannerTypeObj[scope.row.bannerType] }}
            </template>
          </el-table-column>
          <el-table-column prop="bannerStatus" label="轮播图可用状态">
            <template slot-scope="scope">
              {{ BannerStatusObj[scope.row.bannerStatus] }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="edit(scope.row)"
                >编辑</el-button
              >

              <el-button size="mini" type="text" @click="DelUser(scope.row)"
                >删除</el-button
              >

              <el-button
                size="mini"
                type="text"
                @click="Takedown(scope.row, 'Y')"
                :disabled="scope.row.bannerStatus === 'Y' ? true : false"
              >
                启用
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="Takedown(scope.row, 'N')"
                :disabled="scope.row.bannerStatus === 'N' ? true : false"
              >
                禁用
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 新建banner -->
      <el-dialog
        :visible.sync="bannerVisible"
        width="500px"
        :title="title"
        class="no-dialog-padding"
        @close="CancelBanner"
      >
        <span>
          <div class="source_box">
            <el-form
              :model="CreateBannerForm"
              :rules="CreateBannerRules"
              ref="CreateBannerForm"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="门店" prop="storeId">
                <el-select
                  v-model="CreateBannerForm.storeId"
                  placeholder="请选择门店名称"
                  filterable
                  class="w-100"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in $store.state.user.stores"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="排序" prop="bannerSeq">
                <el-input-number
                  v-model="CreateBannerForm.bannerSeq"
                  :max="1000000"
                  :controls="false"
                  class="w-100 text-left"
                  :min="0"
                ></el-input-number>
              </el-form-item>
              <el-form-item label="地址" prop="bannerUrl">
                <up-files
                  :accept="'png,jpg,jpeg'"
                  :size="1"
                  v-model="CreateBannerForm.bannerUrl"
                  @change="changeFile('bannerUrl')"
                >
                </up-files>
                <!-- <el-input v-model="CreateBannerForm.bannerUrl"></el-input> -->
              </el-form-item>
              <el-form-item label="描述" prop="bannerDesc">
                <el-input
                  v-model="CreateBannerForm.bannerDesc"
                  :max="20"
                  class="w-100"
                ></el-input>
              </el-form-item>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="CancelBanner">取 消</el-button>
          <el-button
            type="primary"
            @click="bannerSubmit"
            :loading="CreatebannerLoading"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>
  <script>
import {
  getBannerPage,
  CreateBanner,
  UpdateStoreBanner,
  getBanners,
  deleteBanner,
  StatementInfo,
} from "@/api/business/banner";
import UpFiles from "@/components/UpFiles";
export default {
  name: "EryuoaStoreBanner",
  components: { UpFiles },
  data() {
    return {
      StoreBannerList: [], // 门店banner列表数据
      CreateBannerForm: {
        bannerDesc: "",
        storeId: "",
        bannerUrl: [],
        bannerSeq: "",
        bannerId: "",
      },
      CreateBannerRules: {
        bannerUrl: [
          {
            min: 1,
            required: true,
            type: "array",
            message: "请上传banner图！",
            trigger: "change",
          },
        ],
        bannerSeq: [{ required: true, message: "请输入排序" }],
      },
      BannerTypeObj: {
        S: "门店",
        A: "总部",
      },
      BannerStatusObj: {
        Y: "可用",
        N: "不可用",
      },
      BannerTypeList: [
        {
          value: "S",
          label: "门店",
        },
        { value: "A", label: "总部" },
      ],

      bannerVisible: false, // 新增
      loading: false,
      title: "",
      total: 0,
      CreatebannerLoading: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        bannerType: "",
        storeId: "",
      },
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    changeFile(name) {
      this.$refs["CreateBannerForm"].validateField(name);
    },
    // 启用禁用
    Takedown(row, bannerStatus) {
      this.$confirm(
        "是否确认" + (bannerStatus === "Y" ? "启用" : "禁用") + "?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          StatementInfo({
            bannerId: row.bannerId,
          }).then((res) => {
            const { code } = res;
            if (code === 0) {
              this.getList();
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            }
          });
        })
        .catch(() => {});
    },
    // 获取banner详情
    getBanner(bannerId) {
      getBanners({
        bannerId: bannerId,
      })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            // this.CreateBannerForm = data;
            this.CreateBannerForm.bannerDesc = data.bannerDesc;
            this.CreateBannerForm.bannerId = data.bannerId;
            this.CreateBannerForm.bannerSeq = data.bannerSeq;
            this.CreateBannerForm.storeName = data.storeName;
            this.CreateBannerForm.storeId = data.storeId;

            if (data.bannerUrl) {
              this.CreateBannerForm.bannerUrl = [
                {
                  fileUrl: data.bannerUrl,
                  fileType: this.getFileType(data.bannerUrl).type,
                  fileName: this.getFileType(data.bannerUrl).name,
                },
              ];
            } else {
              this.CreateBannerForm.bannerUrl = [];
            }
          }
        })
        .catch((err) => {});
    },

    getFileType(img) {
      let nameArry = img.split("/");
      let name = nameArry[nameArry.length - 1];
      let typeArry = name.split(".");
      let type = typeArry[typeArry.length - 1];
      console.log(type);
      return {
        type,
        name,
      };
    },

    // 门店后台banner
    getList() {
      // let query = JSON.parse(JSON.stringify(this.queryParams));
      // if (!!this.queryParams.time) {
      //   query.startDate = this.queryParams.time[0];
      //   query.endDate = this.queryParams.time[1];
      //   query.time = [];
      // }
      // delete query.time;
      getBannerPage(this.queryParams)
        .then((res) => {
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.StoreBannerList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    CancelBanner() {
      this.resetFormData("CreateBannerForm");
      this.CreatebannerLoading = false;
      this.$nextTick(() => {
        this.bannerVisible = false;
      });
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
    },

    // 确定
    bannerSubmit() {
      this.$refs["CreateBannerForm"].validate((valid) => {
        if (valid) {
          let CreateBannerForm = JSON.parse(
            JSON.stringify(this.CreateBannerForm)
          );
          CreateBannerForm.bannerId = this.CreateBannerForm.bannerId;
          CreateBannerForm.bannerUrl = !!CreateBannerForm.bannerUrl[0]
            ? CreateBannerForm.bannerUrl[0].fileUrl
            : "";
          if (!!this.CreateBannerForm.bannerId) {
            this.UpdateBanner(CreateBannerForm);
          } else {
            this.CreateBannerImg(CreateBannerForm);
          }
        } else {
          return false;
        }
      });
    },

    // 新增
    CreateBannerImg(CreateBannerForm) {
      CreateBanner(CreateBannerForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.CancelBanner();
            this.$message.success("操作成功");
            this.getList();
          }
        })
        .catch((err) => {});
    },

    // 编辑
    UpdateBanner(CreateBannerForm) {
      CreateBannerForm.bannerId = this.CreateBannerForm.bannerId;
      UpdateStoreBanner(CreateBannerForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.CancelBanner();
            this.$message.success("操作成功");
            this.getList();
          }
        })
        .catch((err) => {});
    },

    // 新建标签
    NewBanner() {
      this.bannerVisible = true;
      this.title = "新增Banner";
      this.CreateBannerForm.bannerId = "";
    },

    // 编辑
    edit(row) {
      this.bannerVisible = true;
      this.title = "编辑Banner";
      this.getBanner(row.bannerId);
    },

    // 表单重置
    resetFormData(formName) {
      this.$refs[formName].resetFields();
    },

    // 删除标签
    DelUser(row) {
      this.$confirm("确认要删除该门店banner吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteBanner({ bannerId: row.bannerId })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getList();
                this.$modal.msgSuccess("操作成功");
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
  },
};
</script>
          
  <style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}
.source_box {
  margin: 10px 24px;
}
.ipt_left {
  padding-right: 25px;
}

.imgBanner {
  width: 60px;
  height: 60px;
}
</style>
          
        
        